/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.koron.purpose.domain.SafetyOrg;

public interface SafetyOrgMapper {

	@Insert("insert into safety_org(id,add_user,add_user_name,add_time,update_user,update_user_name,update_time,corp_code,name,code,pid,ranking,subnumber) "
			+ " values(#{id},#{addUser},#{addUserName},#{addTime},#{updateUser},#{updateUserName},#{updateTime},#{corpCode},#{name},#{code},#{pid},#{ranking},#{subnumber})")
	Integer insert(SafetyOrg bean);

	@Delete("delete from safety_org where id=#{id}")
	Integer delete(@Param("id") String id);

	@Update("update safety_org set update_user=#{updateUser},update_user_name=#{updateUserName},update_time=#{updateTime},"
			+ "name=#{name},subnumber=#{subnumber},ranking=#{ranking} where id=#{id}")
	Integer update(SafetyOrg bean);

	@Select("select * from safety_org where id=#{id}")
	SafetyOrg queryById(@Param("id") String id);

	@Select("select count(*) from safety_org where name=#{name} and corp_code=#{corpCode} and pid=#{pid} ")
	Integer addVerification(@Param("name") String name,@Param("corpCode") String corpCode,@Param("pid") String pid);

	@Select("select count(*) from safety_org where name=#{name} and corp_code=#{corpCode} and pid=#{pid} and id!=#{id}")
	Integer updateVerification(@Param("name") String name,@Param("corpCode") String corpCode,@Param("pid") String pid,@Param("id") String id);

	@Select("select * from safety_org where corp_code=#{corpCode} order by ranking asc")
	List<SafetyOrg> queryByCorp(@Param("corpCode") String corpCode);

	@Select("<script>" +
			"select * from safety_org where corp_code=#{corpCode}" +
			"<if test=\"pid != null and pid != ''\"> and pid=#{pid} </if>"  +
			"<if test=\"pid == null or pid == ''\"> and pid is null </if>"  +
			"</script>")
	List<SafetyOrg> queryByCorpAndPid(@Param("corpCode") String corpCode,@Param("pid") String pid);

	@Select("select count(*) from safety_org where pid=#{id}")
	Integer queryByPid(@Param("id") String id);

	@Select("select count(*) from safety_org_staff where org_id=#{id}")
	Integer queryStaffCount(@Param("id") String id);

	@Select("select count(*) from safety_org where corp_code=#{corpCode}")
	Integer queryRoot(@Param("corpCode") String corpCode);
}
